/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 morale_icon_window.cpp

	$Header: /heroes4/morale_icon_window.cpp $

	$NoKeywords: $

 ************************************************************************/
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "morale_icon_window.h"

#include "bitmap_group_cache.h"
#include "creature_array.h"
#include "external_string.h"
#include "format_string.h"

// -----------------------------------------------------------------
// class to display & give information about morale
// -----------------------------------------------------------------
extern t_bitmap_group_cache const	k_morale_icons_44;
extern t_bitmap_group_cache const	k_morale_icons_34;
t_bitmap_group_cache const			k_morale_icons_44( "icons.morale.44" );
t_bitmap_group_cache const			k_morale_icons_34( "icons.morale.34" );
t_external_string const				k_text_morale( "morale.dialog" );
t_external_string const				k_text_neutral_morale( "neutral_morale.dialog" );
t_external_string const				k_text_morale_help( "morale.help" );

static t_bitmap_group_ptr get_icons( int height )
{
	if (height < 44)
		return k_morale_icons_34.get();
	return k_morale_icons_44.get();
}

t_morale_icon_display::t_morale_icon_display( t_screen_rect const& rect, t_window* parent,
											  int value )
                     : t_bitmap_group_window( get_icons( rect.height() ), rect.top_left(),
					                         0, parent )
{
	set_value( value );
	set_right_click_text( k_text_morale_help );
}


void t_morale_icon_display::set_value( int morale )
{
	std::string help_text;

	if (morale < -10)
		morale = -10;
	if (morale > 10)
		morale = 10;
	set_frame( format_string( "%i morale", morale ) );
	if (morale != 0)
		help_text = format_string( "%+i ", morale ) + k_text_morale.get();
	else
		help_text = k_text_neutral_morale;
	set_help_balloon_text( help_text );
}

t_morale_icon_window::t_morale_icon_window( t_screen_rect const& rect, t_window* parent )
                    : t_morale_icon_display( rect, parent )
{
	m_creature = 0;
	m_army = 0;
}

// -----------------------------------------------------------------
// class to display & give information about morale
// -----------------------------------------------------------------

void t_morale_icon_window::set_creature( t_creature_stack const* creature, 
										 t_creature_array const* army )
{
	if (m_creature == creature && m_army == army)
		return;
	m_creature = creature;
	m_army = army;
	update();

}

// -----------------------------------------------------------------
// class to display & give information about morale
// -----------------------------------------------------------------
void t_morale_icon_window::update()
{
	if (m_creature == 0 || m_army == 0)
		return;

	set_value( m_creature->get_morale( *m_army ) );
}

